package com.company.OfferGuide;

public class 字符串压缩 {

    public static void main(String[] args) {
        System.out.println(compressString("aa"));
    }

    public static String compressString(String S) {

        if (S == null || S.length() == 0) return S;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < S.length();) {
            int j = i + 1;
            int count = 1;
            sb.append(S.charAt(i));
            for (; j < S.length(); j++) {
                if (S.charAt(i) == S.charAt(j)) count++;
                else break;
            }
            i = j;
            sb.append(count);
        }


        return sb.length() >= S.length() ? S : sb.toString();
    }

}
